package api

import (
	"github.com/EDDYCJY/go-gin-example/pkg/app"
	"github.com/gin-gonic/gin"
)

type Specification struct {
	VmGpuCount uint32 `json:"gpu_count"`
}

type SpecificationRequest struct {
	NodeId   string `json:"node_id"` //后面根据实际的情况动态获取,目前未让用户选择
	GpuCount uint32 `json:"gpu_count" binding:"required" binding:"min=1"`
}

type SpecificationResponse struct {
	VmCpuCores uint32 `json:"cpu_cores"`
	VmMemSize  uint64 `json:"mem_size"` //内存（MB）
	VmGpuCount uint32 `json:"gpu_count"`
	VmDiskSize uint64 `json:"disk_size"` //磁盘（GB）
}

// Calculate 计算Cpu核心数计算相应的各个参数的规格值
// curl -X GET -H "Content-Type: application/json" -d '{"gpu_count":1}' 127.0.0.1:9000/machine/rent/specification
func (s *Specification) Calculate() *SpecificationResponse {
	resp := CreateSpecificationResponse()

	resp.VmCpuCores = uint32(s.VmGpuCount * 14)

	resp.VmMemSize = uint64(s.VmGpuCount * 64 * 1024)

	//磁盘计算价格*100
	resp.VmDiskSize = uint64(s.VmGpuCount * 100)
	resp.VmGpuCount = s.VmGpuCount

	return resp
}

func CreateSpecificationResponse() *SpecificationResponse {
	return &SpecificationResponse{}
}

// 计算价格
func Handle_Specification(c *gin.Context) {
	appG := app.Gin{C: c}
	var req SpecificationRequest

	if err := c.ShouldBindJSON(&req); err != nil {
		appG.ResponseError(-1, err.Error())
		return
	}
	spec := Specification{VmGpuCount: req.GpuCount}

	resp := spec.Calculate()

	appG.ResponseSuccess(resp)
}
